<?php
/*
// "K2" Component by JoomlaWorks for Joomla! 1.5.x - Version 2.1
// Copyright (c) 2006 - 2009 JoomlaWorks Ltd. All rights reserved.
// Released under the GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
// More info at http://www.joomlaworks.gr and http://k2.joomlaworks.gr
// Designed and developed by the JoomlaWorks team
// *** Last update: September 9th, 2009 ***
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

class JElementComcontent extends JElement
{
	var	$_name = 'comcontent';
	function fetchElement($name, $value, &$node, $control_name) {	  
	    $cId = JRequest::getVar('cid','');
	    if($cId !='') $cId = $cId[0];
	    if($cId == ''){
	      $cId = JRequest::getVar('id');
	    }
	   $doc =& JFactory::getDocument();
	   $jPathCom = JPATH_COMPONENT;	  
	   $positionPatCom = strrpos($jPathCom,'com_modules');	  
	   $jPathK2 = substr($jPathCom,0,$positionPatCom).'com_k2';
	   $mitems[] = JHTML::_('select.option','0','- '.JText::_('Select').' -');     
	   $mitems[] = JHTML::_('select.option','default_content',JText::_('Default Content'));
	   if(is_dir($jPathK2)){
	     $mitems[] = JHTML::_('select.option','k2_content',JText::_('K2 Content'));
	   }		
		$baseURL = JURI::base();
		$position = strrpos($baseURL,'administrator');
		$url = substr($baseURL,0,$position);		
		$ajax = $url."modules/mod_jv_drawer/elements/ajax.php";		
        $js = "window.addEvent('domready',function(){                    			
                   var comcontent = $('paramscom_content').value;
                   var rowImg = $('paramsremove_k2_img1').getParent().getParent();                                  
                   var urlRequest = '$ajax'; 
                   urlRequest +='?cat='+comcontent+'&view=default&cid=$cId';
                   var request = new Json.Remote(urlRequest,{onComplete:function(jsonObj){                      
                        $('paramscatid').setProperty('name','params[catid][]');                        
	                    if(jsonObj.isDefaultCont == 1){
	                       rowImg.setStyle('display','none');                    
                           jsonObj.jsonData.each(function(item,index){                   
                           var newOptGroup = new Element('optgroup');                                                       
                           newOptGroup.label = item.section;                   
                           item.category.each(function(itemCat,index){                   
                              var newoption = new Element('option');
                              newoption.value = itemCat.category_id;                        
                              newoption.appendChild(document.createTextNode(itemCat.category_name));
                              if(jsonObj.cIdConfig.contains(itemCat.category_id)) newoption.selected = true;                             
                              newOptGroup.adopt(newoption);                 
                       })                                        
                      $('paramscatid').adopt(newOptGroup);                     
                    })      
	               } else {	 
	                       if(jsonObj.tagHtml == 1) {
	                           rowImg.setStyle('display',''); 
	                       } else {
	                           rowImg.setStyle('display','none');
	                       }                                                    
                           $('paramscatid').empty();                           
                           jsonObj.jsonData.each(function(item,index){                   
                              catk2_title = item.catk2_title.replace(/&nbsp;/g, '\u00A0');                   
                              newoption = new Option(catk2_title,item.catk2_id);                                
                              if(jsonObj.cIdConfig.contains(item.catk2_id)) newoption.selected = true;                            
                              $('paramscatid').add(newoption,null);        
                            }); 
	                    }                   
                   }}).send();  
                   $('paramstag_html0').addEvent('click',function(){	  
                     if($('paramstag_html0').checked==true){
	                   var rowContImg = $('paramsremove_k2_img1').getParent().getParent();
	                   rowContImg.setStyle('display','none');
                     }    
                   }) 
                   $('paramstag_html1').addEvent('click',function(){
	                   if($('paramstag_html1').checked==true){
	                       var rowContImg = $('paramsremove_k2_img1').getParent().getParent();
	                       if($('paramscom_content').value =='k2_content') rowContImg.setStyle('display','');
	                   }
                   })
                   
                });
                function selectContent(){ 
                var rowContImg = $('paramsremove_k2_img1').getParent().getParent();                	               
	            $('paramscatid').empty();	           
	            if($('paramscom_content').value =='0'){ 
	               $('paramscatid').empty();
	               rowContImg.setStyle('display','none');	               
	            } else {
	            if($('paramscom_content').value == 'k2_content' && $('paramstag_html1').checked == true){rowContImg.setStyle('display','');
	            } else {
	               rowContImg.setStyle('display','none');
	            }	            
	            urlRequest = '$ajax';
	            var cat = $('paramscom_content').value; 
	            urlRequest +='?cat='+cat+'&view=change_content';
	            var request = new Json.Remote(urlRequest,{onComplete:function(jsonObj){
	            $('paramscatid').setProperty('name','params[catid][]');
	            if(jsonObj.isDefaultCont == 1){              
	                jsonObj.jsonData.each(function(item,index){	                  
	                   var newOptGroup = new Element('optgroup');	                                                    
	                   newOptGroup.label = item.section;	               
	                   item.category.each(function(itemCat,index){                   
	                      var newoption = new Element('option');
	                      newoption.value = itemCat.category_id;	                    
	                      newoption.appendChild(document.createTextNode(itemCat.category_name));	                        
	                      newOptGroup.adopt(newoption);                 
	                   })	                   	                 
	                  $('paramscatid').adopt(newOptGroup);	                   
	                })             
	            }else {	               
	               $('paramscatid').empty();
	                jsonObj.jsonData.each(function(item,index){	                  
	                  catk2_title = item.catk2_title.replace(/&nbsp;/g, '\u00A0');	                 
	                  newoption = new Option(catk2_title,item.catk2_id);
	                   $('paramscatid').add(newoption,null);        
	                }); 
	           }  	          
	          }}).send();          
	        }}";	                  
           $doc = & JFactory::getDocument();
           $doc->addScriptDeclaration($js);		
		return JHTML::_('select.genericlist',  $mitems, ''.$control_name.'['.$name.'][]', 'onchange="selectContent();" class="inputbox"', 'value', 'text', $value );
	}	
}
